package com.huobao.yunying.adapters;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_Ask_Tuihuo;
import com.huobao.yunying.activitys.Act_Dingdan_detail;
import com.huobao.yunying.activitys.Act_Tuikuan_detail;
import com.huobao.yunying.activitys_store.Act_Store;
import com.huobao.yunying.entity.DingdanData;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class DingdanManagerAdapter extends QuickAdapter<DingdanData> {

	private String type;

	public DingdanManagerAdapter(Context context, String type) {
		super(context, R.layout.item_dingdan_manager);
		this.type = type;
	}

	@Override
	protected void convert(BaseAdapterHelper arg0, final DingdanData arg1) {
		arg0.setText(R.id.tv_storename, "店铺：" + arg1.seller_name + ">");
		arg0.setOnClickListener(R.id.tv_storename, new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, Act_Store.class);
				intent.putExtra("id", arg1.seller_id);
				context.startActivity(intent);
			}
		});
		View status_view = arg0.getView(R.id.llyt_1);
		TextView tv_status = arg0.getView(R.id.tv_status);

		final TextView tv_del = arg0.getView(R.id.tv_del);
		TextView tv_fukuan = arg0.getView(R.id.tv_fukuan);
		NoScrollListView listView = arg0.getView(R.id.listview);
		Adapter_dingdan_manager_Item adapter = new Adapter_dingdan_manager_Item(context, arg1.good_data);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				Intent intent;
				if (type == null) {
					if (position == getCount())
						return;
					intent = new Intent(context, Act_Tuikuan_detail.class);
					intent.putExtra("id", getItem(position).order_id);
					context.startActivity(intent);
					return;
				}
				if (position == getCount())
					return;
				intent = new Intent(context, Act_Dingdan_detail.class);
				intent.putExtra("order_id", arg1.order_id);
				context.startActivity(intent);
			}
		});
		int status = Integer.parseInt(arg1.status);

		if (arg1.good_data != null && arg1.good_data.size() != 0) {
			status_view.setVisibility(View.VISIBLE);
		} else {
			status_view.setVisibility(View.GONE);
		}

		tv_del.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(tv_del.getText().toString().equals("取消订单")){
					BaseDialog.TBox("确定取消？", context, new BaseDialog.OnDialogSelectListener() {
						@Override
						public void onSelect(String input) {
							if(input.equals("true")){
								Map<String, String> map = HttpUtil.getParams("app", "buyerorder", "act", "cancel_order", "order_id",
										arg1.order_id);
								Http.getInstant().sendRequest(context, map, new HttpListener() {

									@Override
									public void onHttpSuccess(String result) {
										Toast.makeText(context, "取消成功", Toast.LENGTH_SHORT).show();
										getList().remove(arg1);
										notifyDataSetChanged();
									}

									@Override
									public void onHttpError(String error) {

									}
								});
							}
						}
					});
				}else if(tv_del.getText().toString().equals("退款/退货")){
					if(arg1.good_data == null || arg1.good_data.size() == 0){
						Toast.makeText(context, "订单没有商品", Toast.LENGTH_SHORT).show();
					}else{
						Intent intent = new Intent(context, Act_Ask_Tuihuo.class);
						intent.putExtra("order_id", arg1.order_id);
						intent.putExtra("goods_id", arg1.good_data.get(0).goods_id);
						context.startActivity(intent);
					}
				}
			}
		});
		tv_fukuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Map<String, String> map = HttpUtil.getParams("app", "cashier", "order_id", "[" + arg1.order_id + "]");
				Http.getInstant().sendRequest(context, map, new HttpListener() {

					@Override
					public void onHttpSuccess(String result) {
						JSONObject jsonObject = JSON.parseObject(result);
						jsonObject = jsonObject.getJSONObject("retval");
						String order_amount = jsonObject.getString("order_amount");
						String order_sn = jsonObject.getString("order_sn");
						SkipUtils.Pay(context, order_sn, order_amount, arg1.good_data.get(0).goods_name);
					}

					@Override
					public void onHttpError(String error) {
						// TODO Auto-generated method stub

					}
				});
				// SkipUtils.Pay(context, arg1.order_id,
				// arg1.good_data.get(0).price,
				// arg1.good_data.get(0).goods_name);
			}
		});

		switch (status) {
		// [0已取消 11待付款 10已提交 20待发货 30已发货 40已完成]
		case 0:
			tv_status.setText("状态：已取消");
			tv_del.setVisibility(View.GONE);
			tv_fukuan.setVisibility(View.GONE);
			break;
		case 11:
			tv_status.setText("状态：待付款");
			tv_del.setVisibility(View.VISIBLE);
			tv_fukuan.setVisibility(View.VISIBLE);
			tv_del.setText("取消订单");
			break;
		case 10:
			tv_status.setText("状态：已提交 ");
			tv_del.setVisibility(View.VISIBLE);
			tv_fukuan.setVisibility(View.GONE);
			break;
		case 20:
			tv_fukuan.setVisibility(View.GONE);
			tv_status.setText("状态：待发货");
			tv_del.setVisibility(View.VISIBLE);
			tv_del.setText("退款/退货");
			break;
		case 30:
			tv_status.setText("状态：已发货");
			tv_del.setVisibility(View.GONE);
			tv_fukuan.setVisibility(View.GONE);
			break;
		case 40:
			tv_status.setText("状态：已完成");
			tv_del.setVisibility(View.GONE);
			tv_fukuan.setVisibility(View.GONE);
			break;
		}

	}

}
